function [likel,ssvec,flag_ok,lht]=likelNanReg(parest,parvec,parposest,funcmod,Y,trainvec,solveopt,addsol)
% =========================================================================
%% LikelNanReg.m
%
% This code estimates the likelihood of a 
% 1) NaN model 
% 2) Without Mixed Frequency 
% 3) Regular, i.e. NO Split in matrices
%
% [GG, RR, CONS, eu, SDX, ZZ, initss, ssvec, flag, ssnames]=feval(funcmod,param,options,addsol);
%
% =========================================================================
%% Initialize parameters
likel=-1e20; flag_ok=0;
parvec(parposest)=parest;
%% Model solution, second sample stored in structure second (second sample)
[G,R,C,eu,SDX,Z,~,ssvec,structTwo]=feval(funcmod,parvec,solveopt,addsol);
% A. Non-existence in either sample
if isequal(eu,[1;1])==0 
    return
end
[T,ny]=size(Y);
%% Demeaning 
if any(C~=0)==1;Y=Y-repmat((Z*C)',[T 1]);end;
Y=Y';

%% Initialization
try
    % B. Problems with initialization
    pshat=feval(@lyapunov_symm,G,R*(SDX')*SDX*(R'));
    if ~isempty(find(isnan(pshat))~=0)
        disp('PSHAT contains Nan entries')
        return
    end
catch
    disp('Warning! Problems with initialization in GENSYSPOST_VEC');
    return;
end
shat       = zeros(size(G,1),1);
lht        = zeros(T,1);
Zdim       =  zeros(T,1); 
mat_obspos = zeros(T,ny);
W          = eye(ny);
% Filter
for ii=1:T;
    % Handling of missing observations 
    ytt=Y(:,ii);
    
    % Determine W and position of the NAN  
    ind =~isnan(ytt);
    rowt =find(~isnan(ytt));
    
    ytt=ytt(ind);    
    Zdim(ii)=length(ytt); 
    Ztt=W((ind==1),:)*Z;
    mat_obspos(ii,1:Zdim(ii))=rowt; 
    
    [shat,pshat,lht(ii)]=feval(@kf,ytt,Ztt,shat,pshat,G,R*(SDX'));
end

%% Likelihood plus integration constant

lht   = lht(trainvec(1):trainvec(2));
likel = -0.5*log(2*pi)*(sum(Zdim))+sum(lht);
flag_ok=1;

%% End of File
end